# $Id: RadioButton.pm,v 1.2 2004/04/27 14:26:22 ibo Exp $

package CGI::FOGE2::RadioButton;
use strict;

our @ISA = qw(CGI::FOGE2::MultQuestion);

use CGI::FOGE2::Tools;
use CGI::FOGE2::Stats;

=head1 NAME

RadioButton - a FOGE question where choices are displayed as
radio-buttons or checkboxes. 

=head1 DESCRIPTION

Such a choice is displayed as radio-buttons if ismultiple is false,
it is displayed as independent checkboxes if ismultiple is true. The
rows and columns properties allow to select the appearance of these
buttons or boxes. By default, a single row is produced.

=head1 BUG

When ismultiple is true, this class appropriately inherits from
MultQuestion, but it should rather inherit from Question when
ismultiple is false. I mixed these aspects in order to compare
different solutions.

=cut

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;

    my %args = @_;
    my @mandatory_keys = ('title', 'items', 'id');
    my $self =
      {
       id            => undef,  # autogen (?)
       default       => undef,	# the default item (was: initial)
       items         => undef,  # an array of items (was: choice)
       title         => undef,	# was: label
       ismultiple    => 0,      # That is, radio-buttons
       html_type     => 'radio_group',
       rows          => 1,
       columns       => undef,
       ismandatory   => 0,
       isvalidregexp => undef,
       validate      => undef,  # func. returning string or false
       stats         => [CGI::FOGE2::StatsSimple->new()],
      };

    bless ($self, $class);
    $self->_construct(\%args, \@mandatory_keys);
    $self->{columns} = 1+$#{@{$self->{items}}} unless defined($self->{columns});
    $self->{html_type} = 'checkbox_group' if $self->{ismultiple};
    return $self->init();
}

####### BRICOLAGE #############
sub suitable_stats {
    my $self = shift;
    my %stat_constructors = ('StatsPiechart' => sub {CGI::FOGE2::StatsPiechart->new()},
			     'StatsSimple' => sub {CGI::FOGE2::StatsSimple->new()},
			     );
    return \%stat_constructors;
}
###############################

sub serve {
    my $self = shift;

    my $id = $self->{id};
    my $names = $self->generate_field_names();
    my $name = $names->[0];

    my @items = @{$self->{items}};

    my %line = (-values => \@items, 
                -name => $name,
                -rows => $self->{rows},
                -columns => $self->{columns},
                );

    if (defined $self->{default}) {
	$line{-default} = $self->{default};
    } else {
	$line{-default} = '-'; # deselect all buttons
    }
    
    my $fn = \&{"CGI::" . $self->{html_type}};
    return table3cols($self->{title},
		      $self->{ismandatory},
		      &$fn(%line));
}

# Sort the result of stats->computedata() along the order suggested by
# the items of the question. Code similar to Menu.pm

sub sort {
    my $self = shift;
    my ($number, $total, $names, $vals) = @_;
    my (@new_names, @new_vals);
    my (@lost_names, @lost_vals);
    N: 
    for ( my $i=0 ; $i<@{$names} ; $i++ ) {
        for ( my $j=0 ; $j<@{$self->{items}} ; $j++ ) {
            if ( $self->{items}->[$j] eq $names->[$i] ) {
                $new_names[$j] = $names->[$i];
                $new_vals[$j] = $vals->[$i];
                next N;
            };
        }
        push @lost_names, $names->[$i];
        push @lost_vals, $vals->[$i];
    }
    return ($number, $total, 
            [@new_names, @lost_names], 
            [@new_vals, @lost_vals] );
}

1;

# end of RadioButton.pm
